$('.datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "customer/list",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
		 {
			render : function(data,type, row, meta) {
                return '<a href="'+contextpath+'customer/detail/'+row["id"]+'">'+row["name"]+'</a>';
            }
		 },
		 {
				render : function(data,type, row, meta) {
	                return row['level'] + '级';
	            }
			 },
	     {"data" : "recommendDistributorName"},
         { "data": "phoneNo" },
         {
            render : function(data,type, row, meta) {
                return $("#btnGroup").html();
            }
        }
     ],
     "drawCallback": function( settings ) {
		 
		 $(".btn-sms").click(function () {
			 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			 $(":input[name='customerId']").val(row.id);
         });
      }
});

function selectedSmsTemplet(obj){
	var selectedObj = obj.options[obj.options.selectedIndex];
	var content = $("#content");
	if(selectedObj){
		var value = selectedObj.value;
		if(value == '0'){
			content.val("会员,您好！提醒您请按预约时间yyyy年MM月dd日到我院进行术后拆线。地址：深圳市南山区前海路1128号（千郡医疗）。如有任何疑问，请及时与您的服务顾问联系。祝您生活愉快！");
		}else if(value == '1'){
			content.val("会员,您好！提醒您请按预约时间yyyy年MM月dd日到我院进行复诊。地址：深圳市南山区前海路1128号（千郡医疗）。如有任何疑问，请及时与您的服务顾问联系。祝您生活愉快！");
		}else if(value == '2'){
			content.val("会员,您好！提醒您请按预约时间yyyy年MM月dd日到我院进行操作。地址：深圳市南山区前海路1128号（千郡医疗）。如有任何疑问，请及时与您的服务顾问联系。祝您生活愉快！");
		}else if(value == '3'){
			content.val("千郡会员,在这特别的日子里，千郡医疗祝您生日快乐，幸福相伴！生日当月可凭身份证在我院领免费领取生日神秘礼物一份。美好的祝愿送给美丽的您！");
		}else {
            content.val("会员,");
		}
	}
};

function selectedSendNow(obj){
	var selectedObj = obj.options[obj.options.selectedIndex];
	if(selectedObj){
		var value = selectedObj.value;
	    var text = selectedObj.text;
	    
		if(value == '0'){
			$("#sendTimeDiv").css("display", "none");
			$("#hourDiv").css("display", "none");
		}else{
			$("#sendTimeDiv").css("display", "block");
			$("#hourDiv").css("display", "block");
		}
	}
};

$(function(){
	 
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});	

	$('#birthday').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  "parentEl":$("#edit_dialog"),
				  showDropdowns: true,
				  startDate: moment().subtract('years', 30),
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#birthday').val("");
	
	 
	 $("#submitSms").click(function(){
			if($("#saveSms").valid()){
				$.post(contextpath + "customer/saveSms", $("#saveSms").serialize(), function(){
					swal("发送/保存短信信息成功","", "success");
					$('.close').click();
				})
			}
		});

		 $('#sendTime').daterangepicker(
				  { 
					  "singleDatePicker": true,
					  "parentEl":$("#sms_dialog"),
				    locale:{
				    	format: 'YYYY-MM-DD',
			            applyLabel: '确认',
			            cancelLabel: '取消',
			            fromLabel: '从',
			            toLabel: '到',
			            customRangeLabel: '选择时间',
			            daysOfWeek:["日","一","二","三","四","五","六"],
			            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
			        }
				  }
				);
})
